package com.company.client;

import com.company.common.Message;
import com.company.common.User;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.time.LocalDateTime;

public class UserService {

    public static boolean register(User user) {
        boolean flag = false;
        try {
            Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8080);
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            Message<User> obj = new Message(user, Message.MessageType.REGISTER, "", user.getName(), LocalDateTime.now());
            outputStream.writeObject(obj);
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) inputStream.readObject();
            switch (message.getType()) {
                case REGISTER_SUCCEED:
                    flag = true;
                    break;
                default:
                    inputStream.close();
                    outputStream.close();
                    socket.close();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean login(User user) {
        boolean flag = false;
        try {
            Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8080);
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            Message<User> obj = new Message(user, Message.MessageType.LOGIN, "", user.getName(), LocalDateTime.now());
            outputStream.writeObject(obj);
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) inputStream.readObject();
            switch (message.getType()) {
                case LOGIN_SUCCEED:
                    flag = true;
                    ConnSvrThread thread = new ConnSvrThread(socket);
                    ConnSvrThdManage.add(user.getName(), thread);
                    thread.start();
                    break;
                default:
                    inputStream.close();
                    outputStream.close();
                    socket.close();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static void getOnline(String username) {
        try {
            Socket socket = ConnSvrThdManage.get(username).getSocket();
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            Message<String> obj = new Message("", Message.MessageType.GET_ONLINE_FRIENDS, "", username, LocalDateTime.now());
            outputStream.writeObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void exit(String username) {
        try {
            Socket socket = ConnSvrThdManage.get(username).getSocket();
            Message<String> obj = new Message("", Message.MessageType.CLI_EXIT, "", username, LocalDateTime.now());
            new ObjectOutputStream(socket.getOutputStream()).writeObject(obj);
            ConnSvrThdManage.remove(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
